﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;

namespace StudentApartMentManage
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        

        private void button2_Click(object sender, EventArgs e)
        {
           
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            //登录界面
            if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("用户ID不可为空!");
                return;
            }
            if (textBox2.Text.Length == 0)
            {
                MessageBox.Show("密码不可为空!");
                return;
            }
            
           
            if(comboBox1.Text.Trim()=="普通管理员" || comboBox1.Text.Trim()=="超级管理员")
            {
                string sql0 = string.Format("select 密码 from 公寓管理员信息表 where 管理员ID='{0}'", textBox1.Text.Trim());
                SqlDataReader r = sqlHelper.GetReader(sql0);
                if (r.Read())
                {

                    if (r.GetString(0).Trim() == textBox2.Text.Trim())
                    {
                        if (comboBox1.Text.Trim() == "普通管理员")
                        {
                            UsualAdmin u = new UsualAdmin(textBox1.Text.Trim());
                            u.Show();
                            this.Hide();
                        }
                        else
                        {
                            SuperAdminMainForm sp = new SuperAdminMainForm(textBox1.Text.Trim());
                            sp.Show();
                            this.Hide();

                        }
                       

                    }
                    else
                    {
                        MessageBox.Show("用户ID或密码错误!");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("数据库操作失败!");
                }
                r.Close();
            }
            else
            {
                string sql1 = string.Format("select 登录密码 from 学生信息表 where 学号='{0}'", textBox1.Text.Trim());
                SqlDataReader r = sqlHelper.GetReader(sql1);
                if (r.Read())
                {

                    if (r.GetString(0).Trim() == textBox2.Text.Trim())
                    {
                        StuMainForm stu = new StuMainForm(textBox1.Text.Trim());
                        stu.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("用户ID或密码错误!");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("数据库操作失败!");
                }
                r.Close();
            }
           


        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.ExitThread();
            }
        }
    }
}
